import type { CourseScheduleLessonDateGroup, LessonGroupLessonItem } from '@/api/types/lesson'
import type { LessonInfo } from './type'
import { getCourseTypeName } from '@/enums/enum-mapping'
import { formatClassTimeSimple, formatLessonDuration } from '@/utils/format'

export function mergeLessonResponseData(data: CourseScheduleLessonDateGroup[]) {
  const mergedData: Record<string, CourseScheduleLessonDateGroup> = {}
  const result = []

  for (const item of data) {
    if (mergedData[item.lessonDate]) {
      mergedData[item.lessonDate].lessons.push(...item.lessons)
    }
    else {
      mergedData[item.lessonDate] = { ...item }
    }
  }
  // 将合并后的数据转换为数组
  for (const key in mergedData) {
    result.push(mergedData[key])
  }
  return result
}

// 上课弹框信息 格式化
export function formatLessonInfo(lesson: LessonGroupLessonItem): LessonInfo {
  return {
    studentName: `${lesson.student.name}(${lesson.student.username})`,
    lessonTime: formatClassTimeSimple(lesson.scheduledStartTime, lesson.scheduledEndTime),
    lessonDuration: `${formatLessonDuration(lesson.scheduledStartTime, lesson.scheduledEndTime)}`,
    lessonCourse: lesson.course.name,
    lessonType: getCourseTypeName(lesson.type),
  }
}
